﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TracNghiemTinHocDaiCuong.ThucThe;
using TracNghiemTinHocDaiCuong.BIZ;
using TracNghiemTinHocDaiCuong.Common;

namespace TracNghiemTinHocDaiCuong
{
    public partial class QuanLyNguoiDung : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IList<NguoiDung> danhSachNguoiDung = NguoiDungBIZ.LayDanhSach();
            BindDuLieu_HienThiThongBao(danhSachNguoiDung);
        }

        private void BindDuLieu_HienThiThongBao(IList<NguoiDung> danhSachNguoiDung)
        {
            if (danhSachNguoiDung.Count == 0)
                lblThongBao.Text = "Chưa có bản ghi để hiển thị";
            else
                lblThongBao.Text = "";
            gvDanhSachNguoiDung.DataSource = danhSachNguoiDung;
            gvDanhSachNguoiDung.DataBind();
        }

        protected void gvDanhSachNguoiDung_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDanhSachNguoiDung.PageIndex = e.NewPageIndex;
            gvDanhSachNguoiDung.DataBind();
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            string loc;
            if (ViewState["loc"] == null)
                loc = "";
            else
                loc = ViewState["loc"].ToString();
            IList<NguoiDung> danhSachNguoiDung = NguoiDungBIZ.TimKiem(txtTenTaiKhoan.Text,
                txtHoVaTen.Text, loc);
            BindDuLieu_HienThiThongBao(danhSachNguoiDung);
        }

        protected void btnXoaTuKhoa_Click(object sender, EventArgs e)
        {
            txtTenTaiKhoan.Text = "";
            txtHoVaTen.Text = "";
        }

        protected void ClickFilter(object sender, CommandEventArgs e)
        {
            ViewState["loc"] = e.CommandName;
            LocDanhSachNguoiDung(e.CommandName);
        }

        private void LocDanhSachNguoiDung(string phamVi)
        {
            IList<NguoiDung> danhSachNguoiDung = new List<NguoiDung>();
            switch (phamVi)
            {
                case "All":
                    {
                        danhSachNguoiDung = NguoiDungBIZ.LayDanhSach();
                        break;
                    }
                case "09":
                    {
                        danhSachNguoiDung = NguoiDungBIZ.TimKiem(txtTenTaiKhoan.Text, txtHoVaTen.Text,
                            "[0-9]%");
                        break;
                    }
                case "AE":
                    {
                        danhSachNguoiDung = NguoiDungBIZ.TimKiem(txtTenTaiKhoan.Text, txtHoVaTen.Text,
                            "[A-E]%");
                        break;
                    }
                case "FJ":
                    {
                        danhSachNguoiDung = NguoiDungBIZ.TimKiem(txtTenTaiKhoan.Text, txtHoVaTen.Text,
                            "[F-J]%");
                        break;
                    }
                case "KN":
                    {
                        danhSachNguoiDung = NguoiDungBIZ.TimKiem(txtTenTaiKhoan.Text, txtHoVaTen.Text,
                            "[K-N]%");
                        break;
                    }
                case "OR":
                    {
                        danhSachNguoiDung = NguoiDungBIZ.TimKiem(txtTenTaiKhoan.Text, txtHoVaTen.Text,
                            "[O-R]%");
                        break;
                    }
                case "SV":
                    {
                        danhSachNguoiDung = NguoiDungBIZ.TimKiem(txtTenTaiKhoan.Text, txtHoVaTen.Text,
                            "[S-V]%");
                        break;
                    }
                case "WZ":
                    {
                        danhSachNguoiDung = NguoiDungBIZ.TimKiem(txtTenTaiKhoan.Text, txtHoVaTen.Text,
                            "[W-Z]%");
                        break;
                    }
            }
            BindDuLieu_HienThiThongBao(danhSachNguoiDung);
        }

        protected void btnThemNguoiDung_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThemNguoiDung.aspx");
        }

        protected void btnSuaNguoiDung_Click(object sender, EventArgs e)
        {
            Object id = ViewState["idNguoiDung"];
            if (id == null)
                lblThongBao.Text = "Bạn chưa chọn người dùng";
            else
                Response.Redirect(string.Format("SuaNguoiDung.aspx?ID={0}", ViewState["idNguoiDung"]));
        }

        protected void btnXoaNguoiDung_Click(object sender, EventArgs e)
        {
            if (ViewState["idNguoiDung"] == null)
                lblThongBao.Text = "Bạn chưa chọn người dùng";
            else
                NguoiDungBIZ.Xoa(Methods.ConvertNumber(ViewState["idNguoiDung"].ToString()));
            LayDanhSach();      
        }

        private void LayDanhSach()
        {
            gvDanhSachNguoiDung.DataSource = NguoiDungBIZ.LayDanhSach();
            gvDanhSachNguoiDung.DataBind();
        }

        protected void lkbtnNguoiDung_Click(object sender, CommandEventArgs e)
        {
            hdfThongBao.Value = e.CommandName;
            ViewState["idNguoiDung"] = Methods.ConvertNumber(e.CommandName);

            int i = Methods.ConvertNumber(e.CommandArgument.ToString());
            
            gvDanhSachNguoiDung.Rows[i].Cells[0].CssClass = "row";
            gvDanhSachNguoiDung.Rows[i].Cells[1].CssClass = "row";
            gvDanhSachNguoiDung.Rows[i].Cells[2].CssClass = "row";
            gvDanhSachNguoiDung.Rows[i].Cells[3].CssClass = "row";
            gvDanhSachNguoiDung.Rows[i].Cells[4].CssClass = "row";
            gvDanhSachNguoiDung.Rows[i].Cells[5].CssClass = "row";
            gvDanhSachNguoiDung.Rows[i].Cells[6].CssClass = "row";
        }
    }
}